<?php
class Database {

	private $log;
	private $host,  $user, $pwd, $db, $db_link;
	private $connect_with_salt;

	function __construct($host, $user, $pw, $db, $salt = false) {
		$this->connect_with_salt = $salt;
		$this->log = new Logger("Database", _LOGPATH_);
		$this->log->setLogLevel(Logger::DEBUG);
		$this->host = $host;
		$this->user = $user;
		$this->pwd = $pw;
		$this->db = $db;

		$this->connect();
		$this->select_db();
		$this->setCharset("utf8");
		$this->log->debug("Connected to ".$this->user."@".$this->db);
	}

	function __destruct(){
		mysql_close($this->db_link);
		$this->log->debug("Connection closed");
	}

	private function connect(){
		if($this->connect_with_salt){
			$this->db_link = mysql_connect($this->host, $this->user, md5(_SALT_.sha1($this->pwd))) or die(mysql_error());
		$this->log->debug("connect with ".$this->user."@".md5(_SALT_.sha1($this->pwd)));
		}else{
			$this->db_link = mysql_connect($this->host, $this->user, $this->pwd) or die(mysql_error());
		}
	}

	private function select_db(){
		mysql_select_db($this->db) or die(mysql_error());
	}

	private function setCharset($encoding){
		mysql_set_charset($encoding);
	}

	private function query($query) {
		$this->log->debug($query);
		$result = mysql_query($query) or die(mysql_error());
		return new Result($result);
	}

	private function secureSQL($argArray){
		$sql = array_shift($argArray);
		foreach ($argArray as &$arg){
			$arg = mysql_real_escape_string($arg);
			$arg = "'".$arg."'";
		}
		$args_string = implode(", ", $argArray);
		eval("\$query = sprintf(\"$sql\", $args_string);");
		return $query;
	}


	public function select(){
		$args = func_get_args();
		$qtype = substr(trim($args[0]),0,6);
		if ($qtype != 'SELECT') {
			return false;
		}
		if (count($args) > 1) {
			$query = $this->secureSQL($args);
		}
		return $this->query($query);
	}

	public function insert(){
		$args = func_get_args();
		$qtype = substr(trim($args[0]),0,6);
		if ($qtype != 'INSERT') {
			return false;
		}
		if (count($args) > 1) {
			$query = $this->secureSQL($args);
		}
		return $this->query($query);
	}


	public function update(){
		$args = func_get_args();
		$qtype = substr(trim($args[0]),0,6);
		if ($qtype != 'UPDATE') {
			return false;
		}
		if (count($args) > 1) {
			$query = $this->secureSQL($args);
		}
		return $this->query($query);
	}


	public function delete(){
		$args = func_get_args();
		$qtype = substr(trim($args[0]),0,6);
		if ($qtype != 'DELETE') {
			return false;
		}
		if (count($args) > 1) {
			$query = $this->secureSQL($args);
		}
		return $this->query($query);
	}
}

class Result {
	var $result;

	function Result($result){
		$this->result = $result;
	}

	public function getResult() {
		if ($this->result) {
			$rows = array();
			while($row = mysql_fetch_assoc($this->result)) {
				$rows[] = (object) $row;
			}
			return $rows;
		} else {
			return false;
		}
	}

	public function getRow($query = null) {
		if ($this->result) {
			if ($row = mysql_fetch_row($this->result)) {
				return $row;
			}
		}
		return false;
	}

	public function getRowNum($query = null) {
		if ($this->result) {
			return mysql_num_rows($this->result);
		}
		return 0;
	}
}